<template>
	<view class="placeOrder">
		<view @click="addresschoose" class="placeOrder_address">
			<view class="placeOrder_address_left">
				<image src="../../../static/home/addressicon.png" mode="widthFix"></image>
			</view>
			<view v-if="shippingAddress == null" class="placeOrder_address_right">
				<view class="placeOrder_address_right_left">
					<view class="placeOrder_address_right_title">
						<view style="width: 100%;" class="placeOrder_address_right_title_left">
							请填写收货地址
						</view>
					</view>
				</view>
				<view class="placeOrder_address_right_bottom_right">
					<image src="../../../static/home/right.png" mode="widthFix"></image>
				</view>
			</view>
			<view v-if="shippingAddress !== null" class="placeOrder_address_right">
				<view class="placeOrder_address_right_left">
					<view class="placeOrder_address_right_title">
						<view class="placeOrder_address_right_title_left">
							{{shippingAddress.name}}
						</view>
						<view class="placeOrder_address_right_title_right">
							{{shippingAddress.phone}}
						</view>
					</view>
					<view class="placeOrder_address_right_bottom">
						{{shippingAddress.allAddressInfo}}
					</view>
				</view>
				<view class="placeOrder_address_right_bottom_right">
					<image src="../../../static/home/right.png" mode="widthFix"></image>
				</view>
			</view>
		</view>

		<view v-if="goosform!== null" class="placeOrder_commodity">
			<view class="placeOrder_commodity_detail">
				<view class="placeOrder_commodity_detail_left">
					<image :src="goosform.mainImages" mode=""></image>
				</view>
				<view class="placeOrder_commodity_detail_right">
					<view class="placeOrder_commodity_detail_right_left">
						<view class="placeOrder_commodity_detail_right_title">
							{{goosform.name}}
						</view>
						<view class="placeOrder_commodity_detail_right_bottom">
							<text v-for="(item,index) in form.goodsSpecificationsValue">{{item.boxName}}:{{item.name}};
							</text>
						</view>
					</view>
					<view class="placeOrder_commodity_detail_right_right">
						<view class="placeOrder_commodity_detail_right_rightnum">
							<text class="placeOrder_commodity_detail_right_rightnum_left">￥</text><text
								v-if="isvip==1">{{goosform.memberPrice}}</text><text
								v-if="isvip==0">{{goosform.price}}</text>
						</view>
						<view class="placeOrder_commodity_detail_right_rightqua">
							×{{num || 1}}
						</view>
					</view>
				</view>
			</view>
			<view class="placeOrder_commodity_quantity">
				<view class="placeOrder_commodity_quantity_left">
					购买数量
				</view>
				<view class="placeOrder_commodity_quantity_right">
					<u-number-box min="1" :max="goosform.num" v-model="num"></u-number-box>
				</view>
			</view>
			<view class="placeOrder_commodity_quantity">
				<view class="placeOrder_commodity_quantity_left">
					订单备注
				</view>
				<view class="placeOrder_commodity_quantity_right">
					<u--textarea style="width: 100%;padding: 0;" v-model="remarks" placeholder="请输入订单备注">
					</u--textarea>
				</view>
			</view>
		</view>
		<view v-if="youhuiquan()" class="placeOrder_commodity">
			<view @click="showtow = true" style="margin-top: 0;" class="placeOrder_commodity_quantity">
				<view class="placeOrder_commodity_quantity_left">
					优惠券
				</view>
				<view style="align-items: center;" class="placeOrder_commodity_quantity_right">
					<text class="coupon" v-if="radio">￥{{couponNum}}</text>
					<image style="width:18upx" src="../../../static/home/right.png" mode="widthFix"></image>
				</view>
			</view>
		</view>
		<view v-if="goosform!== null" class="placeOrder_buttonBox">
			<view class="placeOrder_buttonBox_box">
				<view class="placeOrder_buttonBox_box_left">
					共1件，
				</view>
				<view class="placeOrder_buttonBox_boxNum">
					合计：<text class="placeOrder_buttonBox_boxNum_left">￥</text><text v-if="isvip == 1"
						class="placeOrder_buttonBox_boxNum_right">{{(goosform.memberPrice * num) - (Number(couponNum) || 0)}}</text><text
						v-if="isvip == 0"
						class="placeOrder_buttonBox_boxNum_right">{{(goosform.price * num) - (Number(couponNum) || 0)}}</text>
				</view>
				<view class="placeOrder_buttonBox_buttonBox">
					<button :disabled="buttonLoading" @click="confirm" class="placeOrder_buttonBox_button">提交</button>
				</view>
			</view>
		</view>
		<u-popup :show="showtow" :round="true" @close="showtow = false" @open="open" mode="bottom">
			<view class="modeBox">
				<scroll-view :cancelable="false" style="height: 100%;" scroll-y="true">
					<u-radio-group :borderBottom="true" placement="column" v-model="radiostorage" iconPlacement="right">
						<u-radio name="" class="modeBox_radio_list" activeColor="red">不使用</u-radio>
						<u-radio labelColor="#ff0000" v-for="(item , index ) in apicouponarr" :key="index"
							:index="index" :name="item.id" class="modeBox_radio_list" activeColor="red">
							￥{{item.amountNum}}</u-radio>
					</u-radio-group>
				</scroll-view>
				<view class="modeBox_bottom">
					<button @click="youhuiquanqueding" class="modeBox_button">提交</button>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import {
		getinfo
	} from '@/api/mine'
	import {
		bootreceivingAddressselectdefault,
		bootgoodsselectdetail,
		bootorderinsert,
		apicouponlist
	} from '@/api/home'
	export default {
		data() {
			return {
				radio: '',
				couponNum: '',
				radiostorage: '',
				showtow: false,
				user: JSON.parse(uni.getStorageSync('user')),
				form: {},
				goosform: null,
				shippingAddress: null,
				num: 1,
				remarks: '',
				buttonLoading: false,
				apicouponarr: [],
				maxnum:false,
				isvip:0,
			}
		},
		onLoad(data) {
			this.getinfo();
			this.apicouponlist();
			this.form = JSON.parse(data.data)
			
			this.bootreceivingAddressselectdefault()
			this.bootgoodsselectdetail()
			if(this.form.isLimit == 1){
				this.maxnum = Number(this.form.purchaseLimitNum);
			}
		},
		onShow() {
			let address = uni.getStorageSync('addresschoose')
			if (address) {
				let obj = JSON.parse(address);
				this.shippingAddress = obj
				uni.setStorageSync('addresschoose', undefined);
			}
		},
		watch: {
			num(val) {
				if (val < 200) {
					this.radiostorage = ''
					this.couponNum = ''
					this.radio = ''
				}
			}
		},
		methods: {
			getinfo() {
				getinfo().then(res => {
						if (res.success == 1) {
							this.isvip = res.data.isVip;
							console.log(res.data)
						} else {
							uni.showToast({
								icon: 'none',
								title: res.message
							})
						}
					})
					.catch(err => {
			
					});
			},
			open() {

			},
			youhuiquan() {
				let num = 0
				if (this.goosform == null) return false
				if (this.isvip == 1) {
					num = this.goosform.memberPrice * this.num
				} else {
					num = this.goosform.price * this.num
				}
				if (this.goosform !== null && Number(num) >= 200) {
					return true
				} else {
					return false
				}
			},
			youhuiquanqueding() {
				this.radio = this.radiostorage
				this.showtow = false;

				if (this.radiostorage) {
					let this_ = this;
					var queryData = this.apicouponarr.filter(function(fp) {
						return fp.id === this_.radiostorage
					})
					this.couponNum = queryData[0].amountNum
				} else {
					this.couponNum = ''
				}

			},
			apicouponlist() {
				apicouponlist().then(res => {
					console.log(res)
					this.apicouponarr = res.data
				})
			},
			addresschoose() {
				uni.navigateTo({
					url: '/pages/mine/AddressManagement?type=2'
				});
			},
			bootgoodsselectdetail() {
				bootgoodsselectdetail(this.form.goodsId).then(res => {
					this.goosform = res.data;
					if(this.form.isLimit == 1){
						this.goosform.num = this.maxnum;
					}
				})
			},
			confirm() {
				if (this.shippingAddress == null) {
					this.addresschoose()
					return
				}
				let arr = []
				let arrs = []
				for (var i = 0; i < this.form.goodsSpecificationsValue.length; i++) {
					let el = this.form.goodsSpecificationsValue[i]
					console.log(el)
					arr.push(el.value)
					arrs.push(el.name)
				}
				let obj = {
					goodsId: this.goosform.id,
					goodsSpecificationsIds: arr.join(','),
					goodsSpecificationsValue: arrs.join(','),
					num: this.num,
					receivingAddress: this.shippingAddress.allAddressInfo,
					receivingAddressPhone: this.shippingAddress.phone,
					receivingAddressName: this.shippingAddress.name,
					remarks: this.remarks,
					couponAmountId: this.radio
				}
				this.buttonLoading = true
				bootorderinsert(obj).then(res => {
					console.log(res);
					if (res.success == 1) {
						uni.redirectTo({
							url: '/pages/index/payment/payment?id=' + res.data.id
						});
					}else{
						uni.showToast({
							icon: 'none',
							title: res.message
						})
					}
				}).finally(res => {
					this.buttonLoading = false
					// console.log(res);
				})
			},
			bootreceivingAddressselectdefault() {
				bootreceivingAddressselectdefault().then(res => {
					console.log(res)
					this.shippingAddress = res.data
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	* {
		box-sizing: border-box;
	}

	.placeOrder {
		padding: 20upx;

		.coupon {
			color: #ED2525;
			font-weight: 700;
			margin-right: 20upx;
		}

		.modeBox {
			height: 70vh;
			padding: 32upx;
			padding-bottom: 140upx;

			.modeBox_radio_list {
				padding: 24upx 0 !important;
				border-bottom: 1px solid #dadbde;

				/deep/ .u-radio__text {
					font-weight: 700;
				}
			}

			.modeBox_bottom {
				padding: 24upx;

				.modeBox_button {
					background-color: #ED2525;
					color: #ffffff;
					border-radius: 140upx;
					width: 100%;
					height: 100%;
					font-weight: 700;

					&::after {
						display: none;
					}

					&[disabled] {
						background-color: #D8D8D8;
						color: #000000;
					}
				}
			}

		}

		.placeOrder_address {
			display: flex;
			align-items: center;
			background-color: #ffffff;
			border-radius: 20upx;
			padding: 20upx 20upx;

			.placeOrder_address_left {
				width: 100upx;
				margin-right: 20upx;

				image {
					width: 100%;
				}
			}

			.placeOrder_address_right {
				width: calc(100% - 20upx);
				display: flex;
				align-items: center;

				.placeOrder_address_right_left {
					width: calc(100% - 20upx - 18upx);
					margin-right: 20upx;

					.placeOrder_address_right_title {
						display: flex;
						align-items: center;

						.placeOrder_address_right_title_left {
							width: 220upx;
							overflow: hidden;
							text-overflow: ellipsis;
							white-space: nowrap;
							font-size: 36upx;
							color: rgba(0, 0, 0, 0.8);
						}

						.placeOrder_address_right_title_right {
							font-size: 28upx;
							font-family: PingFangSC-Regular, PingFang SC;
							font-weight: 400;
							color: rgba(0, 0, 0, 0.54);
							margin-left: 20upx;
						}
					}

					.placeOrder_address_right_bottom {
						display: flex;
						align-items: center;

						// width: calc(100% - 18upx - 20upx);
						// margin-right: 20upx;
						text-overflow: -o-ellipsis-lastline;
						overflow: hidden;
						text-overflow: ellipsis;
						display: -webkit-box;
						-webkit-line-clamp: 2;
						line-clamp: 2;
						font-size: 28upx;
						-webkit-box-orient: vertical;


					}
				}

				.placeOrder_address_right_bottom_right {
					width: 18upx;
					font-size: 0;

					image {
						width: 18upx;
					}
				}
			}
		}

		.placeOrder_commodity {
			margin-top: 20upx;
			background-color: #ffffff;
			border-radius: 20upx;
			padding: 20upx 20upx;

			.placeOrder_commodity_detail {
				display: flex;

				.placeOrder_commodity_detail_left {
					width: 180upx;
					height: 180upx;

					image {
						width: 100%;
						border-radius: 10upx;
						height: 100%;
					}
				}

				.placeOrder_commodity_detail_right {
					width: calc(100% - 180upx - 20upx);
					margin-left: 20upx;
					display: flex;

					.placeOrder_commodity_detail_right_left {
						width: calc(100% - 120upx - 20upx);
						margin-right: 20upx;

						.placeOrder_commodity_detail_right_title {
							text-overflow: -o-ellipsis-lastline;
							overflow: hidden;
							text-overflow: ellipsis;
							display: -webkit-box;
							-webkit-line-clamp: 2;
							line-clamp: 2;
							-webkit-box-orient: vertical;
							font-size: 28upx;
						}

						.placeOrder_commodity_detail_right_bottom {
							font-size: 28upx;
							padding: 6upx;
							color: #999;
							background-color: #f5f5f5;
						}
					}

					.placeOrder_commodity_detail_right_right {
						width: 120upx;

						.placeOrder_commodity_detail_right_rightnum {
							font-size: 28upx;
							color: #000000;
							text-align: right;

							.placeOrder_commodity_detail_right_rightnum_left {
								font-size: 24upx;
								margin-right: 5upx;
							}
						}

						.placeOrder_commodity_detail_right_rightqua {
							font-size: 28upx;
							color: #ccc;
							text-align: right;
							margin-top: 10upx;
						}
					}
				}

			}

			.placeOrder_commodity_quantity {
				display: flex;
				// align-items: ;
				justify-content: space-between;
				margin-top: 20upx;

				.placeOrder_commodity_quantity_left {
					font-size: 30upx;
					width: 160upx;
					text-align: right;
				}

				.placeOrder_commodity_quantity_right {
					width: calc(100% - 160upx - 20upx);
					display: flex;
					justify-content: flex-end;
				}
			}
		}

		.placeOrder_buttonBox {
			position: fixed;
			bottom: 0;
			padding: 20upx;
			height: 170upx;
			width: 100%;
			left: 0;
			display: flex;
			justify-content: flex-end;
			background-color: #fff;
			align-items: center;

			.placeOrder_buttonBox_box {
				display: flex;
				align-items: center;

				.placeOrder_buttonBox_box_left {
					color: #ccc;
					font-size: 34upx;
				}

				.placeOrder_buttonBox_boxNum {
					font-size: 34upx;

					display: flex;
					align-items: center;

					.placeOrder_buttonBox_boxNum_left {
						font-size: 34upx;
						color: #ED2525;
					}

					.placeOrder_buttonBox_boxNum_right {
						font-size: 50upx;
						color: #ED2525;
					}

				}

				.placeOrder_buttonBox_buttonBox {
					margin-left: 32upx;

					.placeOrder_buttonBox_button {
						background-color: #ED2525;
						color: #ffffff;
						border-radius: 174upx;
						width: 174upx;
						height: 74upx;
						line-height: 74upx;

						&::after {
							display: none;
						}

						&[disabled] {
							background-color: #D8D8D8;
							color: #000000;
						}
					}
				}
			}
		}
	}
</style>
<style>
	page {
		background-color: #E5E7E9;
	}
</style>
